﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Peekaboo2008.util;
using Peekaboo2008.model;
using System.Data.SqlClient;

namespace Peekaboo2008.dao
{
    class NhomKhachHangDAO
    {
        public static DataTable getAll()
        {
            return DataUtil.executeStore("sp_NhomKhachHang_GetAll", null);
        }
        public static DataTable getAllOrderByName()
        {
            return DataUtil.executeStore("sp_NhomKhachHang_GetAllOrderByName", null);
        }
        public static DataTable getAllNCC()
        {
            return DataUtil.executeStore("sp_NhomNhaCungCap_GetAll", null);
        }
        public static DataTable getAllNCCOrderByName()
        {
            return DataUtil.executeStore("sp_NhomNhaCungCap_GetAllOrderByName", null);
        }
        public static NhomKhachHangModel getByID(String _ID)
        {
            NhomKhachHangModel model = new NhomKhachHangModel();
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("ID", SqlDbType.Int);
            param[0].Value = _ID;

            DataTable tb = DataUtil.executeStore("sp_NhomKhachHang_GetByID", param);
            try
            {
                foreach (DataRow dr in tb.Rows)
                {
                    model.Id= dr["ID"].ToString();
                    model.Ten = dr["Ten"].ToString();
                    model.ChietKhau = dr["Chiet_khau"].ToString();
                    model.GhiChu = dr["Ghi_chu"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            return model;
        }

        public static Boolean insert(NhomKhachHangModel nhomKhachHangModel)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("Ten", SqlDbType.NVarChar);
            param[0].Value = nhomKhachHangModel.Ten;
            param[1] = new SqlParameter("ChietKhau", SqlDbType.NVarChar);
            param[1].Value = nhomKhachHangModel.ChietKhau;
            param[2] = new SqlParameter("GhiChu", SqlDbType.NVarChar);
            param[2].Value = nhomKhachHangModel.GhiChu;
            param[3] = new SqlParameter("MaLoaiKhachHang", SqlDbType.NVarChar);
            param[3].Value = nhomKhachHangModel.MaLoaiKhachHang;
            return DataUtil.executeNonStore("sp_NhomKhachHang_Insert", param);
        }

        public static Boolean update(NhomKhachHangModel nhomKhachHangModel)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("Ten", SqlDbType.NVarChar);
            param[0].Value = nhomKhachHangModel.Ten;
            param[1] = new SqlParameter("ChietKhau", SqlDbType.NVarChar);
            param[1].Value = nhomKhachHangModel.ChietKhau;
            param[2] = new SqlParameter("GhiChu", SqlDbType.NVarChar);
            param[2].Value = nhomKhachHangModel.GhiChu;
            param[3] = new SqlParameter("ID", SqlDbType.Int);
            param[3].Value = nhomKhachHangModel.Id;
            return DataUtil.executeNonStore("sp_NhomKhachHang_update", param);
        }
    }
}
